package com.xiaobai.units;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.xiaobai.common.FreeClassroomCommon.KXJS;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-11-26
 **/

public class FreeClassroomUtil {

    public static Map<String, Object> buildParamsMap(Date date, int[] paragraphs) {
        return new Request(date, paragraphs).build();
    }

    // 请求体 四项 由日期转变而来(星期,周次) 节次 校区 pageSize100
    private static class Request {

        // 基准只算星期,不算周次(因为新学期周次会乱)
        // 教务网 xq:1 是周日, 即周日为新的一周; xq:7是周六
        // 2021-11-20 00:00:00
        private static Date baseDate = new Date(1637337601000L);
        // 12周
        private static int baseZc = 12;
        // xq为7(实则星期六)
        private static int baseXq = 7;


        // 固定属性
        private static int isqy = 1;
        // 当前周次无关紧要
        private static int curzc = 1;
        // 这个不知道
        private static int zhzc = 1;
        private static String xnxqdm = KXJS;
        private static int page = 1;
        // 所有校区
        private static int rows = 200;


        private int zc;
        private int xq;
        private String jc;

        private Request(Date date, int[] paragraphs) {
            for (int paragraph : paragraphs) {
                if (paragraph > 5 || paragraph < 1) {
                    throw new RuntimeException("节次 [1 ~ 5]");
                }
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < paragraphs.length; i++) {
                if (paragraphs[i] != 5) {
                    sb.append(0).append((paragraphs[i] * 2)).append(",");
                } else {
                    sb.append(paragraphs[i] * 2).append(",");
                }
            }
            sb.setLength(sb.length() - 1);
            this.jc = sb.toString();
            // 计算出 周次 + 星期
            int[] ints = calculationZcXq(date);
            this.zc = ints[0];
            this.xq = ints[1];
        }

        private Map<String, Object> build() {
            Map<String, Object> map = new HashMap<>(10);
            map.put("isqy", isqy);
            map.put("curzc", curzc);
            map.put("zhzc", zhzc);
            map.put("xnxqdm", xnxqdm);
            map.put("page", page);
            map.put("rows", rows);
            map.put("zc", zc);
            map.put("xq", xq);
            map.put("jc", jc);
            return map;
        }

        private static int[] calculationZcXq(Date date) {
            long l = date.getTime() - baseDate.getTime();
            long day = l / 1000 / 60 / 60 / 24;
            // 因为是周六 00:00 所以不需要baseXq
            int xq = (int) (day % 7);
//            int zc = (int) (baseZc + day / 7);
//            zc = xq == 0 ? zc : zc + 1;
            int zc = getZc(date);
            xq = xq == 0 ? 7 : xq;
            return new int[]{zc, xq};
        }

        // 周次Map
        public static Map<Integer, Integer> zcMap = new HashMap<>(1);
        public static String zcSearchUrl = "https://jwxt.gdupt.edu.cn/teajssqxx!getZCXQByRq.action?date=";

        public static int getZc(Date date) {
            int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
            if (zcMap.containsKey(day)) {
                return zcMap.get(day);
            }
            // 新的一天了,去查询吧
            zcMap.clear();
            String url = zcSearchUrl + DateFormatUtils.format(date, "yyyy-MM-dd");
            String dataStr = HttpUnits_V2.commonGyGet(url, null, null);
            int zc = Integer.parseInt(JSONObject.parseObject(dataStr).get("zc").toString());
            zcMap.put(day, zc);
            return zc;
        }

    }

}
